/* hl2demo buffer */

/* Copyright (c) 2007-2012, ben "boin" bonoeil

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.
    * Neither the name of the <organization> nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

    NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
    GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY
    ben "boin" bonoeil ''AS IS'' AND ANY EXPRESS OR IMPLIED
    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL ben "boin" bonoeil BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "hl2demo_buffer.h"
#include "hl2demo_print.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>

struct buffer_s {
  int max;
  int len;
  int lock;
  int kill;
};

#define SIZEOF_BUFFER_T sizeof(struct buffer_s)

buffer_t buffer_create(int size)
{
  buffer_t buf = 0;

  if (size >= 0) {
    const int need = SIZEOF_BUFFER_T + size;
    buf = malloc(need);
    if (buf) {
      buf->max  = size;
      buf->len  = 0;
      buf->lock = 0;
      buf->kill = 0;
    } else {
      error("can't alloc %d bytes buffer (%s)\n", need, strerror(errno));
    }
  }
  return buf;
}

buffer_t buffer_resize(buffer_t buf, int size)
{
  if (!buf) {
    buf = buffer_create(size);
  } else if (size != buf->max) {
    if (buf->lock) {
      error("trying to resize a locked buffer\n");
      buf = 0;
    } else {
      const int need = size + SIZEOF_BUFFER_T;
      buf = realloc(buf, need);
      if (buf) {
        buf->max = size;
        if (buf->len > size) buf->len = size;
      } else {
        error("can't realloc %d bytes buffer (%s)\n", need, strerror(errno));
      }
    }
  }
  return buf;
}

buffer_t buffer_ensure(buffer_t buf, int size)
{
  if (!buf || size < buf->max) {
    buf = buffer_resize(buf, size);
  }
  return buf;
}

buffer_t buffer_ensure_free(buffer_t buf, int size)
{
  const int need = buf->len+size;
  if (!buf || need < buf->max) {
    buf = buffer_resize(buf, need);
  }
  return buf;
}

int buffer_destroy(buffer_t buf, int force)
{
  int ret = -1;

  if (buf) {
    if (buf->lock && !force) {
      warning("delay destruction of a %d time(s) locked buffer\n",buf->lock);
      buf->kill = 1;
      ret = buf->lock;
    } else {
      if (buf->lock) {
        warning("destroying a locked buffer (%d lock(s))\n",buf->lock);
      }
      free(buf);
      ret = 0;
    }
  }
  return ret;
}

void * buffer_lock(buffer_t buf, int * idx, int * len)
{
  if (!buf) return 0;
  if (idx) *idx = buf->len;
  if (len) *len = buf->max;
  ++buf->lock;
  return (char *)(buf+1);
}

int buffer_unlock(void * data)
{
  int ret = -1;
  if (data) {
    buffer_t buf = ((buffer_t)data)-1;
    if  (buf->lock > 0) {
      ret = --buf->lock;
      if (!ret && buf->kill) {
        warning("delayed destruction of buffer\n");
        free(buf);
      }
    }
  }
  return ret;
}

void buffer_reset(buffer_t buf)
{
  if (buf) buf->len = 0;
}

buffer_t buffer_append(buffer_t buf, const void * data, int len)
{
  if (!buf) {
    buf = buffer_create(len);
  }

  if (buf) {
    int need = buf->len+len;
    if (need > buf->max) {
      buf = buffer_resize(buf, need);
    }
    if (buf) {
      char * dst = (char *)(buf+1) + buf->len;
      if (data) memcpy(dst, data, len);
      buf->len = need;
    }
  }
  return buf;
}
